﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using SlimDX.Multimedia;
using SlimDX;
using SlimDX.XAudio2;
using PluginInterfacesLib;

namespace MusicPlugin
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Dictionary<ComboBox, FileInfo> map;  //(although it could be simpler without it i used a) Dictionary (equivalent of Java Map)
                                                      //for storing combobox/sound pairs (it could help us later)
        XAudio2 device;

        public MainWindow()
        {
            InitializeComponent();

            //populate each combobox with all the sounds located in the Sounds folder
            populateComboBoxes();

            //init instance vars
            map = new Dictionary<ComboBox, FileInfo>();
            device = new XAudio2();
            MasteringVoice mvoice = new MasteringVoice(device);
        }

        private void soundComboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //there is no update method so i remove previously stored pair (if it exists)
            map.Remove(soundComboBox1);
            map.Add(soundComboBox1, (FileInfo)soundComboBox1.SelectedItem);
        }

        private void soundComboBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            map.Remove(soundComboBox2);
            map.Add(soundComboBox2, (FileInfo)soundComboBox2.SelectedItem);
        }

        private void soundComboBox3_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            map.Remove(soundComboBox3);
            map.Add(soundComboBox3, (FileInfo)soundComboBox3.SelectedItem);
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            FileInfo sound;
            map.TryGetValue(soundComboBox1, out sound);
            if (sound != null)
            {
                playSound(sound.FullName);
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            FileInfo sound;
            map.TryGetValue(soundComboBox2, out sound);
            if (sound != null)
            {
                playSound(sound.FullName);
            }
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            FileInfo sound;
            map.TryGetValue(soundComboBox3, out sound);
            if (sound != null)
            {
                playSound(sound.FullName);
            }
        }
        private void populateComboBoxes()
        {
            //get current executable directory
            DirectoryInfo soundsDirectory = new DirectoryInfo(System.Reflection.Assembly.GetExecutingAssembly().Location);

            //go up until reaching root of the project
            while (soundsDirectory.Name != "MusicPlugin")
            {
                soundsDirectory = soundsDirectory.Parent;
            }

            //go into Sounds directory
            soundsDirectory = soundsDirectory.GetDirectories("Sounds", SearchOption.TopDirectoryOnly)[0];

            //get all wav files from Sounds directory
            foreach (FileInfo sound in soundsDirectory.GetFiles())
            {
                if (sound.Extension == ".wav")
                {
                    soundComboBox1.Items.Add(sound);
                    soundComboBox2.Items.Add(sound);
                    soundComboBox3.Items.Add(sound);
                }
            }
        }
        private void playSound(string fileName)
        {
            WaveStream stream = new WaveStream(fileName);
            AudioBuffer buffer = new AudioBuffer();
            buffer.AudioData = stream;
            buffer.AudioBytes = (int)stream.Length;
            buffer.Flags = BufferFlags.EndOfStream;

            SourceVoice voice = new SourceVoice(device, stream.Format);
            voice.SubmitSourceBuffer(buffer);
            voice.Start();
        }
    }
}
